//
//  QPHandPickedService.swift
//  QianPing
//
//  Created by yanglipeng on 2018/11/28.
//  Copyright © 2018 yanglipeng. All rights reserved.
//

import UIKit
import Alamofire
import Moya
enum QPCommonService {
    case login(username: String, password: String, flag: Int)
    case uptoken
    case qpInfo(id: Int)
 
    case sendComment(content: String, id: Int, toUserId: Int)
    case sendSubComment(content: String, id: Int, parentId: Int, toUserId: Int)
    case getBulletScreen(id: Int)
    case removeDisincline(id: Int)
    case sendVerityCode(phoneNumber: String, templateId: Int) // 验证码 251156: 注册 254940：修改密码
    case download(filePath: String, downloadDestination: DownloadDestination)
    case reading(id: Int) // 浏览浅评记录
    case searchResultList(searchKey: String) // 搜索结果列表
    case isHavePhone//是否绑定了手机号
    case checkIsComment//检测是否有评论
    case sendShopComment(shopId:Int,couponId:Int,commentType:Int,content:String,userCouponId:Int)//进行商家评论 commentType等级（1好2一般3差)
}


extension QPCommonService: TargetType {
    
    public var baseURL: URL {
        switch self {
        case .login(_, _, _):
           return QPNetWork.loginUrl
        case .download(let filePath, _):
            return URL(string: filePath)!
        default:
            return QPNetWork.baseUrl
        }
        
    }
    public var path: String {
        switch self {
        case .login(_, _, _):
            return "/qianping/userApi/user/api/login"
        case .uptoken:
            return "/qianping/commentsApi/comments/shallowComment/uptoken"
        case .qpInfo(_):
            return "/qianping/commentsApi/comments/shallowComment/qpdetail"
 
        case .sendComment(_, _, _):
            return "/qianping/commentsApi/comments/shallowContentComment/commentPost"
        case .sendSubComment(_, _, _, _):
            return "/qianping/commentsApi/comments/shallowContentComment/sonCommentPost"
        case .getBulletScreen(_):
            return "/qianping/commentsApi/comments/shallowContentComment/BulletScreen"
        case .removeDisincline(let id):
            return "/qianping/commentsApi/comments/disincline/disincline/\(id)"
        case .sendVerityCode(_, _):
            return "/qianping/userApi/user/userInfo/sendCode"
        case .download(_):
            return ""
        case .reading(_):
            return "/qianping/commentsApi/comments/commentQueryRecord/browseLog"
        case .searchResultList(_):
            return "/qianping/shopApi/shop/utils/solr/query"
        case .isHavePhone:
            return "/qianping/userApi/user/userInfo/isHavePhone"
        case .checkIsComment:
            return "/qianping/userApi/user/userCoupon/checkIsComment"
        case .sendShopComment(_, _, _, _, _):
            return "/qianping/shopApi/shop/shopComment/sendShopComment"
            

        }
    }
    public var method: Moya.Method {
        
        switch self {
        case .removeDisincline(_):
            return .post
        case .sendVerityCode(_):
            return .post
        case .reading(_), .searchResultList(_):
            return .post
        case .isHavePhone, .checkIsComment,.sendShopComment(_, _, _, _, _):
            return .post
        default:
            return .get
        }
    }
    public var task: Task {
        switch self {
        case .login(let username, let password, let flag):
            
            var params:[String : Any] = [:]
            
            params["username"] = username
            params["password"] = password
            params["flag"] = flag
            return .requestParameters(parameters: params, encoding: URLEncoding.default)

        case .sendComment(let content, let shallowCommentId, let toUserId):
            var params:[String : Any] = [:]
            
            params["content"] = content
            params["shallowCommentId"] = shallowCommentId
            params["toUserId"] = toUserId
            params["client"] = "ios"
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case .sendSubComment(let content, let shallowCommentId, let parentsId, let toUserId):
            var params:[String : Any] = [:]
            
            params["content"] = content
            params["shallowCommentId"] = shallowCommentId
            params["parentsId"] = parentsId
            
            if toUserId > 0 {
                params["toUserId"] = toUserId
            } else {
                params["toUserId"] = 0
            }
            params["client"] = "ios"
            return .requestParameters(parameters: params, encoding: URLEncoding.default)

        case .getBulletScreen(let id):
            return .requestParameters(parameters: ["shallowCommentId": id], encoding: URLEncoding.default)
        case .sendVerityCode(let phoneNumber, let templateId):
            return .requestParameters(parameters: ["phone": phoneNumber, "templateId": templateId], encoding: URLEncoding.default)
        case .download(_, let downloadDestination):

            return .downloadDestination(downloadDestination)
        case .reading(let id) :
            return .requestParameters(parameters: ["shallowCommentId": id], encoding: URLEncoding.default)
            
        case .searchResultList(let searchKey):
            var params:[String : Any] = [:]
            let lng = ""
            let lat = ""
            
            params["searchKey"] = searchKey
            params["lng"] = lng
            params["lat"] = lat
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case .sendShopComment(let shopId,let couponId,let commentType,let content,let userCouponId):
            var params:[String : Any] = [:]
            
            params["shopId"] = shopId
            params["couponId"] = couponId
            params["commentType"] = commentType
            params["content"] = content
            params["userCouponId"] = userCouponId
            return .requestParameters(parameters: params, encoding: URLEncoding.default)

        default:
            return .requestPlain
        }
    }
    var validationType: ValidationType {
        return .none
    }
    var sampleData: Data {
        
        return "".data(using: String.Encoding.utf8)!
    }
    var headers: [String: String]? {
        return QPNetWork.headers
    }
}
